package com.website.system.controller;

import com.website.common.controller.BaseController;
import com.website.system.dao.UserMapper;
import com.website.system.model.User;
import com.website.system.utils.JuheDemo;
import com.website.system.utils.SendMailText;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 账号注册
 */
@Controller
public class RegisterController extends BaseController {

    @Resource
    private UserMapper userMapper;
    private JuheDemo juheDemo;
    private SendMailText sendMailText;

    /**
     * 跳转到注册页面
     */
    @RequestMapping(value = ("/register"), method = RequestMethod.GET)
    public String register() {
        return "manage/register";
    }

    /**
     * 注册
     */
    @ResponseBody
    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    public String registerUser(@ModelAttribute User user,String birthdaySelect) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            if(birthdaySelect!="" && !birthdaySelect.equals("")){
              Date birthday = format.parse(birthdaySelect);
                user.setBirthday(birthday);
            }
            userMapper.insert(user);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }

    /**
     * 发送短信验证码
     */
    @ResponseBody
    @RequestMapping(value = "/sendSms")
    public Object sendSms(String phone) {
        Object code = juheDemo.sendMessage(phone);
        return code;
    }

    /**
     * 注册成功发送邮箱通知
     **/
    @ResponseBody
    @RequestMapping("/sendEmail")
    public void sendEmail(String emailSend) {
        try {
            sendMailText.getMimeMessage(emailSend);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}